package com.tiancheng.trade.settlement.service.settlement.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.settlement.bo.MerchantQueryBo;
import com.tiancheng.trade.settlement.mapper.merchant.MerchantMapper;
import com.tiancheng.trade.settlement.model.merchant.Merchant;
import com.tiancheng.trade.settlement.service.merchant.IMerchantService;
import com.tiancheng.trade.settlement.util.cache.SettleCache;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Auther: Likailun
 * @Date: 11:13 2021/4/25
 * @Description: 商户信息service
 */
@Service
public class IMerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements IMerchantService {

    @Override
    public List<MerchantQueryBo> findByMerchantCods(List<String> codes) {
        List<MerchantQueryBo> merchantQueryBos = new ArrayList<>();
        for (String code : codes) {
            Optional<MerchantQueryBo> revenueMerchant = SettleCache.getRevenueMerchant(code);
            if(revenueMerchant.isPresent()){
                merchantQueryBos.add(revenueMerchant.get());
                continue;
            }
            List<MerchantQueryBo> allMerchants = this.getBaseMapper().findByMerchantCods(codes);
            for (MerchantQueryBo allMerchant : allMerchants) {
                if(allMerchant.getMerchantCode().equals(code)){
                    merchantQueryBos.add(allMerchant);
                }
                Optional<MerchantQueryBo> isExistence = SettleCache.getRevenueMerchant(allMerchant.getMerchantCode());
                if(!isExistence.isPresent()){
                    SettleCache.preRevenueMerchant(allMerchant.getMerchantCode(),allMerchant);
                }
            }
        }
        return merchantQueryBos;
    }
}
